package com.reagent.system.controller;

import com.alibaba.excel.EasyExcel;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.reagent.response.Result;
import com.reagent.system.entity.Goods;
import com.reagent.system.entity.excel.GoodsExcel;
import com.reagent.system.entity.excel.ImportExcel;
import com.reagent.system.entity.excel.filter.EasyExcelImportFilter;
import com.reagent.system.service.GoodsService;
import com.reagent.system.service.TypeService;
import com.reagent.system.service.UserService;
import com.reagent.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.filechooser.FileSystemView;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class ExcelController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private UserService userService;
    @Autowired
    private EasyExcelImportFilter easyExcelImportFilter;

    @GetMapping("/expToExcel")
    public Object expToExcel() {
        log.info("准备导出...");
        List<Goods> list = goodsService.getBaseMapper().selectList(null);
        List<GoodsExcel> data = new ArrayList<>();
        String token = JWTUtils.getCurrentToken();
        if (!ObjectUtils.isEmpty(token)) {
            log.info("获取token成功...");
            DecodedJWT jwt = JWTUtils.verifyToken(token);
            if (!ObjectUtils.isEmpty(jwt)) {
                log.info("token验证成功...");
                if (!ObjectUtils.isEmpty(list)) {
                    log.info("正在生成导出信息...");
                    for (Goods goods : list) {
                        GoodsExcel excel = new GoodsExcel();
                        String status = "未上架";
                        if (goods.getStatus() == 1) {
                            status = "已上架";
                        }
                        excel.setCreatetime(goods.getCreatetime())
                                .setDesc(goods.getDesc())
                                .setId(goods.getId())
                                .setName(goods.getName())
                                .setType(typeService.getBaseMapper().selectById(goods.getType()).getName())
                                .setNum(goods.getNum())
                                .setPrice(goods.getPrice())
                                .setStatus(status)
                                .setUpdatetime(goods.getUpdatetime())
                                .setUname(userService.getBaseMapper().selectById(goods.getMid()).getUsername());
                        data.add(excel);
                    }
                    log.info("遍历完成...");
                    try {
                        EasyExcel.write(FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath() +
                                "\\商品列表.xlsx", GoodsExcel.class).sheet(
                                "sheetOne").doWrite(data);
                        return Result.success().message("导出成功!");
                    } catch (Exception e) {
                        return Result.error().message("导出失败，请重试!");
                    }
                }
                log.warn("商品列表为空,无法导出!");
                return Result.error().message("商品列表为空，无法导出!");
            }
            log.info("token验证失败...");
        }
        log.error("获取token失败...");
        return Result.error();
    }

    @PostMapping("/importToList")
    public Object importToList(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ImportExcel.class, easyExcelImportFilter).sheet().doRead();
            log.info("成功!");
            return Result.success().message("上传成功!");
        } catch (Exception e) {
            log.error("失败");
            return Result.error().message("上传失败,请重试!");
        }
    }

}
